﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LIMS.SqlServerDAL
{
    /// <summary>
    /// 实验项目服务类
    /// By：田玉粮
    /// 3013.3.20
    /// </summary>
    public class ProjectService : LIMS.IDAL.IProject
    {
        LIMSDataContext DB = new LIMSDataContext();

        /// <summary>
        /// 添加指定的实验项目
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public bool Add(LIMS.Model.Projects project)
        {
            TB_Project proEntity = new TB_Project()
            {
                ID = project.ID,
                proDetail = project.proDetail,
                proData = project.proDate,
                proName = project.proName,
                proTeacherId = project.proTeacherId
            };
            DB.TB_Project.InsertOnSubmit(proEntity);
            DB.SubmitChanges();
            return true;
        }

        /// <summary>
        /// 添加指定的实验项目
        /// </summary>
        /// <param name="project"></param>
        /// <param name="classIDs"></param>
        /// <returns></returns>
        public bool Add(LIMS.Model.Projects project, List<String> classIDs)
        {
            TB_Project proEntity = new TB_Project()
            {
                ID = project.ID,
                proDetail = project.proDetail,
                proData = project.proDate,
                proName = project.proName,
                proTeacherId = project.proTeacherId
            };

            DB.TB_Project.InsertOnSubmit(proEntity);
            DB.SubmitChanges();
            return true;
        }
        /// <summary>
        /// 删除指定的实验项目
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public bool Remove(LIMS.Model.Projects project)
        {
            var proEntity = DB.TB_Project.SingleOrDefault(p => p.ID == project.ID);
            DB.TB_Project.DeleteOnSubmit(proEntity);
            DB.SubmitChanges();
            return true;
        }

        /// <summary>
        /// 获得所有实验信息
        /// </summary>
        /// <returns></returns>
        public List<LIMS.Model.Projects> FindAll()
        {
            List<LIMS.Model.Projects> listEntity = new List<Model.Projects>();
            DB.TB_Project.Select(s => s).ToList().ForEach(
                (item) =>
                {
                    Model.Projects project = new Model.Projects()
                    {
                        ID = item.ID,
                        proName = item.proName,
                        proDate = item.proData.HasValue ? item.proData.Value : DateTime.Now,
                        proDetail = item.proDetail,
                        proTeacherId = item.proTeacherId
                    };
                    listEntity.Add(project);
                });
            return listEntity;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="proID">实验的自增种子ID</param>
        /// <returns></returns>
        public LIMS.Model.Projects FindByID(Int32 proID)
        {
            Model.Projects pro = new Model.Projects();
            var tmp = DB.TB_Project.Single(sg => sg.ID == proID);
            pro.ID = tmp.ID;
            pro.proDate = tmp.proData.HasValue ? tmp.proData.Value : DateTime.Now;
            pro.proDetail = tmp.proDetail;
            pro.proName = tmp.proName;
            pro.proTeacherId = tmp.proTeacherId;
            return pro;
        }

        /// <summary>
        /// 根据实验名称获取实验信息 
        /// </summary>
        /// <param name="strProName">指定的实验名称</param>
        /// <returns></returns>
        public List<LIMS.Model.Projects> FindByProName(String strProName)
        {
            List<LIMS.Model.Projects> listEntity = new List<Model.Projects>();
            DB.TB_Project.Where(w => w.proName.Contains(strProName)).Select(s => s).ToList().ForEach(
                (item) =>
                {
                    Model.Projects project = new Model.Projects()
                    {
                        ID = item.ID,
                        proName = item.proName
                    };
                    listEntity.Add(project);
                });
            return listEntity;
        }


        /// <summary>
        /// 根据实验时间获取实验信息
        /// </summary>
        /// <param name="beginPro">实验开始时间</param>
        /// <param name="endPro">实验结束时间</param>
        /// <returns></returns>
        public List<LIMS.Model.Projects> FindByTime(DateTime beginPro, DateTime endPro)
        {
            List<LIMS.Model.Projects> proTmp = new List<Model.Projects>();
            return proTmp;
        }

        /// <summary>
        /// 根据实验室名称获取实验数据
        /// </summary>
        /// <param name="strLabName">指定的实验室名称</param>
        /// <returns></returns>
        //public List<LIMS.Model.Projects> FindBylabName(String strLabName)
        //{
        //    List<LIMS.Model.Projects> proTmp = new List<Model.Projects>();
        //    return proTmp;
        //}

        /// <summary>
        /// 修改指定的实验项目数据
        /// </summary>
        /// <param name="project">指定待修改的实验项目信息对象实例</param>
        /// <returns></returns>
        public bool Moeify(LIMS.Model.Projects project)
        {
            var proEntity = DB.TB_Project.SingleOrDefault(p => p.ID == project.ID);
            proEntity.proData = project.proDate;
            proEntity.proDetail = project.proDetail;
            proEntity.proName = project.proName;
            proEntity.proTeacherId = project.proTeacherId;

            DB.SubmitChanges();
            return true;
        }

    }

}
